/**
 * 
 */
package lex.proxyverfier;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;

import org.lex.swing.DocumentAdapter;


/**
 * @author Daniel Liu
 */
public class FilterPane extends JPanel
{
	private JTextField textField;
	private JComboBox comboBox;
	private JLabel filterLabel;

	public FilterPane()
	{
		super();
		this.initialize();
		this.hookActionHandler();
	}

	static class FilterTextFieldActionListener implements ActionListener
	{
		@Override
		public void actionPerformed( ActionEvent e )
		{}
	}

	private void hookActionHandler()
	{
		this.getTextField().getDocument().addDocumentListener( new DocumentAdapter(){
			@Override
			public void documentChanged( DocumentEvent e )
			{
				String text = textField.getText().trim();
				if( 0 == text.length() )
					return;
				Pattern p = Pattern.compile( Pattern.quote( text ) );
				int column = getComboBox().getSelectedIndex() + 1;
				ProxyTableModel model = ProxyVerifier.getApplication().getProxyManager();
				int rowCount = model.getRowCount();
				for( int row = 0; row < rowCount; row++ )
				{
					String input = String.valueOf( model.getValueAt( row, column ) );
					Matcher m = p.matcher( input );
					model.setValueAt( m.find(), row, 0 );
				}
			}
		} );
	}

	private void initialize()
	{
		final GridBagLayout gridBagLayout = new GridBagLayout();
		setLayout( gridBagLayout );
		final GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.insets = new Insets( 0, 3, 0, 3 );
		gridBagConstraints.anchor = GridBagConstraints.BASELINE_LEADING;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridx = 0;
		add( getFilterLabel(), gridBagConstraints );
		final GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
		gridBagConstraints_2.insets = new Insets( 0, 0, 0, 5 );
		gridBagConstraints_2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints_2.anchor = GridBagConstraints.BASELINE;
		add( getComboBox(), gridBagConstraints_2 );
		final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
		gridBagConstraints_1.fill = GridBagConstraints.BOTH;
		gridBagConstraints_1.anchor = GridBagConstraints.BASELINE;
		gridBagConstraints_1.weightx = 10.0;
		gridBagConstraints_1.weighty = 1.0;
		gridBagConstraints_1.gridy = 0;
		gridBagConstraints_1.gridx = 2;
		add( getTextField(), gridBagConstraints_1 );
	}

	private JLabel getFilterLabel()
	{
		if( filterLabel == null )
		{
			filterLabel = new JLabel();
			filterLabel.setText( "Filter:" );
		}
		return filterLabel;
	}

	private JComboBox getComboBox()
	{
		if( comboBox == null )
		{
			comboBox = new JComboBox( ProxyTableModel.getCanFilteredColumnNames() );
		}
		return comboBox;
	}

	private JTextField getTextField()
	{
		if( textField == null )
		{
			textField = new JTextField();
		}
		return textField;
	}
}
